package com.jiezuo.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.jiezuo.auth.entity.Position;
import com.jiezuo.auth.entity.User;
import com.jiezuo.auth.service.PositionService;
import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.enums.ResultEnums;
import com.jiezuo.common.util.PageUtil;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.vo.PageVo;
import com.jiezuo.common.vo.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description 岗位
 * @author 庞留杰
 * @date 2024/03/01 15:03
 * @version V1.0
 **/
@Slf4j
@Transactional
@RequestMapping("/position")
@RestController
//岗位", tags = {"岗位接口"})
public class PositionController extends BaseController {

    @Autowired
    private PositionService service;

    //查询带分页列表", notes = "查询带分页列表")
    @GetMapping("/findPage")
    public Result<IPage<Position>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute Position position) {
        IPage<Position> page = service.findPage(PageUtil.initMpPage(pageVo), position);
        return new ResultUtil<IPage<Position>>().setData(page);
    }

    //查询不带分页列表", notes = "查询不带分页列表")
    @GetMapping("/findList")
    public Result<List<Position>> findList(@ModelAttribute Position position) {
        List<Position> list = service.findList(position);
        return new ResultUtil<List<Position>>().setData(list);
    }

    //通过ID查询", notes = "通过ID查询")
    @GetMapping("/findById")
    public Result<Position> findById(String id) {
        Position position = service.findById(id);
        return new ResultUtil<Position>().setData(position);
    }

    //添加", notes = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody Position position) {
        boolean boo = service.add(position);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    //修改", notes = "修改")
    @PostMapping("/update")
    public Result update(@RequestBody Position position) {
        boolean boo = service.update(position);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    //删除", notes = "删除【逻辑删除】")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean boo = service.delete(id);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    // 通过岗位查询用户
    @GetMapping("/findUserListByPositionId")
    public Result<List<User>> findUserListByPositionId(@RequestParam String position_id) {
        List<User> list = service.findUserListByPositionId(position_id);
        return new ResultUtil<List<User>>().setData(list);
    }

}
